using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//后台用户登录
namespace ApiProject.Api.Controllers
{
   
    //管理员登录并获取token
    [ApiController]
    [Route("[controller]")]
    public class LoginController : ControllerBase
    {
        private IRepository<Admins> _adminsRepository;

        private IRepository<Users> _usersRepository;

        private IRepository<AdminInfo> _admininfoRepository;
        private Token _tokenParameter;

        private readonly IConfiguration _configuration;

        public LoginController(
            IConfiguration configuration,
            IRepository<Admins> adminsRepository,
            IRepository<Users> usersRepository,
            IRepository<AdminInfo> admininfoRepository
        )
        {
            _adminsRepository = adminsRepository;
            _configuration = configuration;
            _tokenParameter =
                _configuration.GetSection("TokenParameter").Get<Token>();
            _usersRepository = usersRepository;
            _admininfoRepository = admininfoRepository;
        }

        //管理员登录并获取token
        // Post:/login/admin
        [AllowAnonymous]
        [HttpPost, Route("admin")]
        public dynamic AdminLogin(NewAdmin model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            var admin = _adminsRepository.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault();
            //查找不到
            if (admin == null)
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "用户名或密码错误,请重新输入"
                    });
            }
            else
            {
                //判断管理员是否删除
                if (admin.IsDelete == false)
                {
                    //判断管理员是否启用
                    if (admin.IsEnable == true)
                    {
                        var token = TokenHelper.GetToken(_tokenParameter, admin);
                        var refreshToken = "123456";
                        var admininfo = _admininfoRepository.GetById(admin.Id);


                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 200,
                                Data =
                                    new
                                    {
                                        Token = token,
                                        RefreshToken = refreshToken,
                                        AdminName = username,
                                         Id = admin.Id,
                                         RealName = admininfo.RealName
                                    },
                                Msg = "管理员登录成功，获取token成功"
                            });
                    }
                    else
                    {
                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 1000,
                                Data = "",
                                Msg = "该管理员已被禁用，请联系超级管理员"
                            });
                    }
                }
                else
                {
                    return JsonHelper
                        .Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "该管理员已被删除，请联系超级管理员"
                        });
                }
            }
        }


        //修改用户密码
        [HttpPost("{id}")]
        public dynamic modUserpass(int id, ModAdminPass model)
        {
            var pass = model.beforeusername.Trim();
            var password = model.Password.Trim();
            var CheckPass = model.CheckPass.Trim();
            var userpass = _adminsRepository.Table.Where(x => x.Password == pass).FirstOrDefault();
             System.Console.WriteLine(userpass);
            if (userpass != null)
            {
                 var user = _adminsRepository.GetById(id);
                 if (user != null)
                    {
                        user.Password =password ;

                        user.UpdatedTime = DateTime.Now;
                        _adminsRepository.Update(user);
                        return JsonHelper.Serialize(new
                        {
                            Code = 200,
                            Data = user,
                            Msg = string.Format("修改成功，请注意查收", id)
                        });
                    }
                    else
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 104,
                            Data = "",
                            Msg = "指定Id的用户不存在，请确认后重试"
                        });
                    }


            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "原密码错误，请确认后重试"
                });
            }

        }

    }
}
